Jump to page sections

Open-source PowerShell RSI Calculator (Relative Strength Index)

Initial date of publishing: 2022-11-05.

PowerShell Core runs on Windows, macOS and Linux. This RSI calculator is made in PowerShell. If you are using a Windows computer, you already have Windows PowerShell 5.1 installed, and can use that version.

The relative strength index (RSI) is a momentum indicator used in technical analysis that measures the magnitude of recent price changes to evaluate overbought or oversold conditions in the price of a stock or other asset.

The RSI is displayed as an oscillator (a line graph that moves between two extremes) and can have a reading from 0 to 100. The indicator was originally developed by J. Welles Wilder Jr. and introduced in his seminal 1978 book, “New Concepts in Technical Trading Systems.”

The relative strength index (RSI) is a popular momentum indicator developed in 1978.

The RSI provides technical traders with signals about bullish and bearish price momentum, and it is often plotted beneath the graph of an asset's price. An asset is usually considered overbought when the RSI is above 70 % and oversold when it is below 30 %.

Parameters for the script

ParameterExplanation
NumbersThe numbers to calculate an RSI value for, such as 15 (or 197) stock market close rates, hourly/whatever crypto currency data, or endless other types of data.
FirstStepSampleCountSets the first step sample count to be used (default 14) for calculating RSI step one. If you want standard RSI calculations, leave this parameter out.

The traditional RSI formula uses 14 data points/numbers. This parameter allows you to specify a different count for your RSI calculation. The number of -Numbers you need is this number plus one (to get $FirstStepSampleCount percentages, the first calculated and used percentage requires a "starting number").
GetSecondStepSwitch parameter (takes no value) indicating you want the RSI second step on a larger set of data than without this (only the first RSI step is calculated without this parameter).
SecondStepSampleCount Sets the second step sample count which should be evenly divisible by $FirstStepSampleCount, used for calculating the second step of the RSI formula. Default 14 * 14 (14 times the default $FirstStepSampleCount). 14*14 = 196. If you want standard RSI calculations, leave this parameter out. Only in use together with -GetSecondStep.

The traditional RSI formula uses 14*14 numbers. This parameter allows you to specify a different count for your RSI calculation. If you provide the switch parameter -GetSecondStep, the number of -Numbers you need is this number plus one (to get $SecondStepSampleCount percentages, the first calculated and used percentage requires a "starting number").
You can download the script from the most-used code storage place in the world, GitHub, here.

Example use

PS C:\> $EthPrices = gci "$MyHome\coinmarketcapdata_5m\*.json" | 
    sort-object Name | 
    select-object -last 197 | 
    %{ ((gc -raw $_.fullname) | 
    ConvertFrom-Json).data.where({
    $_.slug -eq 'ethereum'}).quote.usd.price }
PS C:\> $EthPrices.Count
197
PS C:\> $EthPrices[0-1]
1619.804828821538
1588.1437982934824

PS C:\> Get-RSI -Numbers $EthPrices -GetSecondStep

RsiStepOnesCalulationsForStepTwo : {@{Numbers=System.Decimal[]; Percentages=System.Object[]; Gains=System.Object[];
                                   AverageGain=0.0332195783966708; Losses=System.Object[];
                                   AverageLoss=-0.0143569386067286; SampleCount=14; RSIStepOne=69.8234769777225;       
                                   DateTime=10/30/2022 11:11:06 PM}, @{Numbers=System.Decimal[];
                                   Percentages=System.Object[]; Gains=System.Object[];
                                   AverageGain=0.0828497999703561; Losses=System.Object[];
                                   AverageLoss=-0.0259971421649874; SampleCount=14; RSIStepOne=76.1158727521607;       
                                   DateTime=10/30/2022 11:11:06 PM}, @{Numbers=System.Decimal[];
                                   Percentages=System.Object[]; Gains=System.Object[];
                                   AverageGain=0.0182880118722195; Losses=System.Object[];
                                   AverageLoss=-0.0662359869957693; SampleCount=14; RSIStepOne=21.6364726197847;       
                                   DateTime=10/30/2022 11:11:06 PM}, @{Numbers=System.Decimal[];
                                   Percentages=System.Object[]; Gains=System.Object[];
                                   AverageGain=0.0286163046781352; Losses=System.Object[];
                                   AverageLoss=-0.0681690762665304; SampleCount=14; RSIStepOne=29.5667634913746;       
                                   DateTime=10/30/2022 11:11:06 PM}...}
RsiStepOnesString                : 69.82 - 76.12 - 21.64 - 29.57 - 15.51 - 72.93 - 37.96 - 26.01 -
                                   72.43 - 45.24 - 26.99 - 70.55 - 72.06 - 26.68
RsiStepOnesAverage               : 47.39
SecondStepSampleCount            : 196
PreviousXAverageGain             : 0.0387438591514454
PreviousXAverageLoss             : -0.0471697208659479
RSIStepTwo                       : 44.08
Numbers                          : {1619.804828821538, 1619.8921283607126, 1619.9369185512846,
                                   16211149589714098...}
DateTime                         : 10/30/2022 11:11:06 PM

PS C:\>

I recommend including an ISO8601 timestamp in the filename (or a variant of ISO8601, rather) so you can sort on the filename if something happens to the files, they are moved to another disk, etc. My files are named e.g. crypto-json-coinmarketcap-top-100-2022-10-26_21.00.29.json, which technically isn't ISO8601.

The "RsiStepOnesCalulationsForStepTwo" property is there in case you want to perform math operations or similar on the 14 RSI step ones that are combined to get the second step RSI number.

The "RsiStepOnesString" is a convenience property for visual inspection of the 14 RSI step ones that form the RSI step two number, in chronological order.

The average of the RSI step ones is also calculated for potential elucidation.

The "PreviousXAverageGain" number by default is the average gain based on the previous 13 average gain percentages for the RSI step one periods, which are then again averaged to produce this number. The same logic applies to "PreviousXAverageLoss", only for losses.

In the example above, you have 0.0387 as the average gain and -0.04717 as the average loss. This is as expected when the RSI step two is below 50, so it also serves as a sanity-check of the calculations. If the gains are higher than the losses, the RSI step two will be higher than 50. At least typically.

Some other, self-explanatory properties are included for convenience.

If you want to look at periods longer back in time than the minimum ~16.4 hours required with 5-minute samples and 197 files, you can do it in a myriad of ways. One is by skipping every other file (experienced programmers immediately smell the modulus operator here).

Only RSI step one

PS /> $BitcoinPrices3 = gci "$MyHome/coinmarketcapdata_5m/*.json" | 
    Sort-Object Name | 
    Select-Object -Last 15 | 
    %{(gc -raw $_.FullName | ConvertFrom-Json
    ).data.where({$_.slug -eq 'bitcoin'}).quote.usd.price}
PS /> Get-RSI -Numbers $BitcoinPrices3                                                                                                                                                                         
Numbers     : {21336.7507475232, 21329.3592371492, 21328.3559122658, 21328.979918163…}
Percentages : {-0.0346421555065408302976309700, -0.0047039616720061419873462200, 
               0.0029257102599321227143309500, -0.0209321620402393993940203300…}
Gains       : {0.0029257102599321227143309500, 0.013861648396240968074699100, 
              0.0173634991327144277054730400, 0.0388522788372299841613685200…}
AverageGain : 0.00869109795628686
Losses      : {-0.0346421555065408302976309700, -0.0047039616720061419873462200, 
              -0.0209321620402393993940203300, -0.0229687896176396015299950700…}
AverageLoss : -0.0105057203141827
SampleCount : 14
RSIStepOne  : 45.2736377134766
DateTime    : 11/5/2022 11:27:17 PM

PS /> 

A practical application demo

An example use case is to set up push notifications (I use pushover.net) to your phone with hourly development. This can be supplemented with warnings that trigger on threshold values, for instance if there is a big change in percent, as you see fit in your code.

The end result can look something like this:





Powershell      Windows      Finance      Finans      RSI      Math      Stocks      Technical Trading      Aksjer          All Categories

Google custom search of this website only

Minimum cookies is the standard setting. This website uses Google Analytics and Google Ads, and these products may set cookies. By continuing to use this website, you accept this.

If you want to reward my efforts